<?php
/* --------------------------------------------------------------
   gm_save_template_file.inc.php 2010-08-19 mb
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2010 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/
?><?php
	
function gm_save_template_file($id, $content, $lang, $type) {
	$gm_get_template = xtc_db_query("SELECT 
																			filename,
																			folder
																		FROM gm_emails
																		WHERE gm_email_id = '" . $id . "'
																		LIMIT 1");
	$template = xtc_db_fetch_array($gm_get_template);
	@clearstatcache();	
	if(@is_writable(DIR_FS_CATALOG . 'templates/' . CURRENT_TEMPLATE . '/' . $template['folder'] . '/mail/' . $lang . '/' . $template['filename'] . '.' . $type))
    {
        $fp = fopen(DIR_FS_CATALOG . 'templates/' . CURRENT_TEMPLATE . '/' . $template['folder'] . '/mail/' . $lang . '/' . $template['filename'] . '.' . $type, "w+");
        fwrite($fp, stripslashes($content));
        fclose($fp);
        return true;
    }
    else return false;
}

function gm_correct_config_tag($content){
	
	$config_pos = strpos($content, '{config');
	$config_pos2 = 0;
	while($config_pos !== false){
		$config_end_pos = strpos($content, '}', $config_pos2);
		$content1 = substr($content, 0, $config_pos);
		$content2 = substr($content, $config_pos, $config_end_pos-$config_pos+1);
		$content3 = substr($content, $config_end_pos+1, strlen($content)-($config_end_pos+1));
		
		$content = $content1 . str_replace('&quot;', '"', $content2) . $content3;
		
		$config_pos = strpos($content, '{config', $config_pos2);
		if($config_pos !== false){
			$config_pos = strpos($content, '{config', $config_pos+1);
			$config_pos2 = $config_pos+1;
		}
	}
	
	
	$config_pos = strpos($content, '<p>{config');
	$config_pos2 = 0;
	while($config_pos !== false){
		$config_end_pos = strpos($content, '}</p>', $config_pos2);
		$content1 = substr($content, 0, $config_pos);
		$content2 = substr($content, $config_pos+3, $config_end_pos-$config_pos-2);
		$content3 = substr($content, $config_end_pos+5, strlen($content)-($config_end_pos+1));
		
		$content = $content1 . str_replace('&quot;', '"', $content2) . $content3;
		
		$config_pos = strpos($content, '<p>{config', $config_pos2);
		if($config_pos !== false){
			$config_pos = strpos($content, '<p>{config', $config_pos+1);
			$config_pos2 = $config_pos+1;
		}
	}
	
	return trim($content);
	
}
?>